package org.example.utils;

import com.sun.org.apache.xml.internal.security.Init;
import org.example.dao.RouterRuleDao;
import org.example.dao.RouterparametersDao;
import org.example.po.Routerparameters;
import org.example.po.Routerrule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class CodeCache {

    public static Map<String, List<Routerparameters>> routerparametersMap = new HashMap<>();
    public static boolean isUsing = false;

    private static Logger LOGGER = LoggerFactory.getLogger(CodeCache.class);


    @Resource
    private RouterparametersDao routerparametersDao;

    @PostConstruct
    public void init() {
        List<Routerparameters> list= routerparametersDao.selectList(null);
        for (Routerparameters routerparameter: list) {
            String rulename = routerparameter.getRulename();
            String projectname = routerparameter.getProjectname();
            String key = rulename + "_" + projectname;
            List<Routerparameters> list1 = routerparametersMap.getOrDefault(key, new ArrayList<>());
            list1.add(routerparameter);
            routerparametersMap.put(key, list1);
        }
        isUsing = true;
        LOGGER.info("routerparametersMap缓存完成");
    }

    @PreDestroy
    public void destroy() {
        //系统运行结束
    }

    public static List<Routerparameters> getRouterparameters(String key) {
        if (routerparametersMap == null){
            return null;
        }
        return routerparametersMap.get(key);
    }


}

